package com.carsite.webapp.help;

import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.carsite.model.CheckTable;

public class Excel_CheckTableInfo {
	public static boolean createExcelFromBean(CheckTable checkTable,OutputStream os){
		WritableWorkbook wwb = null; 
		boolean ok = true;
		String sheetName="sheet0";
		String[] cellTitles={"货号","货名称","种类","数量"};
		String[] methods={"getSn","getTitle","getType","getNum",};
		try { 
			wwb =  Workbook.createWorkbook(os);
			
			
			//wwb = Workbook.createWorkbook(exlFile);
			
			///
			
			WritableSheet ws = wwb.createSheet(sheetName, 0);

			// 写表头描述
			int row = 0;
			String[] cellTiles0={"仓库名","盘点时间","初盘点人姓名","复盘点人姓名"};
			for(int i = 0, len = cellTiles0.length; i < len; i++){
				Label label = new Label(i, row, cellTiles0[i]); 
				ws.addCell(label);
			}
			row += 1;
			
			String[] cellTile1=new String[4];
			cellTile1[1]=checkTable.getDeptName();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			cellTile1[1]=sdf.format(checkTable.getDTime());
			cellTile1[1]=checkTable.getPerson00_Name();
			cellTile1[1]=checkTable.getPerson01_Name();
			for(int i = 0, len = cellTile1.length; i < len; i++){
				Label label = new Label(i, row, cellTile1[i]); 
				ws.addCell(label);
			}
			row += 1;
			
			
			for (int i = 0, len = cellTitles.length; i < len; i++) {
				Label label = new Label(i, row, cellTitles[i]);

				ws.addCell(label);
			}
			row += 1;
			
			// 写值
			Iterator li =checkTable.getItems().iterator();// list.listIterator();
			while (li.hasNext()) {
				Object bean = (Object)li.next();

				for (int i = 0; i < methods.length; i++) {
					  
					String methodName = methods[i];
					Method method = bean.getClass().getMethod(methodName);

					Object rst = method.invoke(bean, null);

					Label label = new Label(i, row, rst != null ? rst
							.toString() : "");

					ws.addCell(label);
				}

				row += 1;
			}
			
		/*	System.out.println("wwb.toString()");
			System.out.println(wwb.toString()); 
			System.out.println("wwb.toString()");
			File file=new File("");
			wwb.setOutputFile(file);
			file.
			 */
			
			
			wwb.write();
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		} finally {
			if (wwb != null) {
				try {
					wwb.close();
				} catch (Exception e) {

				}
			}
		}

		return ok;
	}
	public CheckTable createBeanFromExcel(){
		return null;
	}
}
